package com.project.fortuneteller.mappers;

import com.project.fortuneteller.pojo.Message;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mapstruct.Mapper;

import java.util.List;
// 好奇怪 有时候 @Mapper注解会失效
@Mapper
public interface MessageMapper {

    @Select("select * from message where sender_id= #{senderId} ")
    List<Message> getMessageById(int senderId);

    @Select("select * from message where sender_id=#{userId}  and receiver_id=#{friendId} or sender_id=#{friendId} and receiver_id=#{userId} order by create_time;")
    List<Message> getWithFriendMessage(@Param("userId") int userId, @Param("friendId") int friendId);

    @Insert("insert into message (sender_id , receiver_id, content,type,status,create_time ,is_group ) values (#{senderId} , #{receiverId}, #{content} ,#{type} ,#{status} , #{createTime}, #{isGroup}) ")
    int insertMessage(Message message);

    @Select("select count(*) from message where sender_id=#{friendId}  and receiver_id=#{userId}  and status = 0 ")
    int getNoReadCount(@Param("userId") int userId, @Param("friendId") int friendId);

    @Select("update message  set status = 1  where receiver_id=#{userId} and sender_id=#{senderId} and status = 0 ")
    void changeMessageStatus(@Param("userId") int userId ,@Param("senderId") int senderId);

}
